package com.easycoding.ums.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 图片验证码
 * 
 * @author linxx
 */
public class ImageServlet extends HttpServlet {

	private static final long serialVersionUID = 2150322994333709368L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int width = 66;// 验证码图片宽度
		int height = 19;// 验证码图片高度
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_3BYTE_BGR);
		Graphics g = image.getGraphics();
		Random random = new Random();// 创建一个随机类
		g.setColor(new Color(0, 0, 0));// 黑色背景
		g.fillRect(0, 0, width, height);// 画背景
		// g.setColor(getRandColor(0,255));//边框颜色
		// g.drawRect(0, 0, width-1, height-1);//画边框
		// g.setColor(getRandColor(160, 200));// 随机产生5条干扰线，使图象中的认证码不易被其它程序探测到
		// for (int i = 0; i < 8; i++) {
		// int x = random.nextInt(width);
		// int y = random.nextInt(height);
		// int x1 = random.nextInt(width);
		// int y1 = random.nextInt(height);
		// g.drawLine(x, y, x1, y1);
		// }
		g.setColor(getRandColor(220, 255));// 随机产生80点，使图象中的认证码不易被其它程序探测到
		for (int i = 0; i < 80; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			g.drawLine(x, y, x, y);
		}
		Font font = new Font("Arial Unicode MS", Font.BOLD, 15); // 创建字体，字体的大小应该根据图片的高度来定。
		g.setFont(font);// 设置字体
		int length = 5; // 设置默认生成5个数字验证码
		String s = "0123456789"; // 设置备选验证码:包括"a-z"和数字"0-9"
		String sRand = "";
		g.setColor(new Color(255, 255, 255));// 白色文字
		for (int i = 0; i < length; i++) {
			String ch = String.valueOf(s.charAt(random.nextInt(s.length())));
			sRand += ch;
			g.drawString(ch, 9 * i + 10, 15);
		}

		// 将生成的字符串存储在session中
		HttpSession session = request.getSession();
		session.setAttribute("captcha", sRand);
		g.dispose();// 图像生效
		// 禁止图像缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");

		// 创建二进制的输出流
		ServletOutputStream sos = response.getOutputStream();
		// 将图像输出到Servlet输出流中。
		ImageIO.write(image, "jpeg", sos);
		sos.flush();
		sos.close();

	}

	public Color getRandColor(int lower, int upper) {
		Random random = new Random();
		if (upper > 255)
			upper = 255;
		if (upper < 1)
			upper = 1;
		if (lower < 1)
			lower = 1;
		if (lower > 255)
			lower = 255;
		int r = lower + random.nextInt(upper - lower);
		int g = lower + random.nextInt(upper - lower);
		int b = lower + random.nextInt(upper - lower);
		return new Color(r, g, b);
	}
}
